package com.nlp.utils;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;

public class HttpClientManager {
    // 读取超时
    private final static int SOCKET_TIMEOUT = 300000;
    // 连接超时
    private final static int CONNECTION_TIMEOUT = 200000;
    // 每个HOST的最大连接数量
    private final static int MAX_CONN_PRE_HOST = 512;
    // 连接池的最大连接数
    private final static int MAX_CONN = 1024;
    // 连接池
    private final static HttpConnectionManager httpConnectionManager;

    private static HttpClient httpClient = null;

    static {
        httpConnectionManager = new MultiThreadedHttpConnectionManager();
        HttpConnectionManagerParams params = httpConnectionManager.getParams();
        params.setConnectionTimeout(CONNECTION_TIMEOUT);
        params.setSoTimeout(SOCKET_TIMEOUT);
        params.setDefaultMaxConnectionsPerHost(MAX_CONN_PRE_HOST);
        params.setMaxTotalConnections(MAX_CONN);
        //params.setStaleCheckingEnabled(true);
        httpClient = new HttpClient(httpConnectionManager);
    }

    public static HttpClient getHttpClient() {
        return httpClient;
    }


}
